// models/order.go
package models

import (
	"time"

	"gorm.io/gorm"
)

// OrderStatus 订单状态枚举
type OrderStatus string

const (
	OrderStatusPending   OrderStatus = "pending"   // 待处理
	OrderStatusConfirmed OrderStatus = "confirmed" // 已确认
	OrderStatusShipped   OrderStatus = "shipped"   // 已发货
	OrderStatusDelivered OrderStatus = "delivered" // 已送达
	OrderStatusCancelled OrderStatus = "cancelled" // 已取消
)

// Order 订单模型
type Order struct {
	ID          uint           `gorm:"primaryKey" json:"id"`
	OrderNo     string         `gorm:"size:50;uniqueIndex;not null" json:"order_no"`    // 订单号
	CustomerID  uint           `gorm:"not null" json:"customer_id"`                     // 客户ID
	Customer    User           `gorm:"foreignKey:CustomerID" json:"customer"`           // 客户信息
	CreatorID   uint           `gorm:"not null" json:"creator_id"`                      // 创建人ID
	Creator     User           `gorm:"foreignKey:CreatorID" json:"creator"`             // 创建人信息
	Status      OrderStatus    `gorm:"size:20;default:'pending'" json:"status"`         // 订单状态
	TotalAmount float64        `gorm:"type:decimal(10,2);not null" json:"total_amount"` // 总金额
	Remark      string         `gorm:"type:text" json:"remark"`                         // 备注
	OrderDate   time.Time      `gorm:"not null" json:"order_date"`                      // 订单日期
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"-"`

	// 关联订单项
	Items []OrderItem `gorm:"foreignKey:OrderID" json:"items"`
}

// OrderItem 订单项模型
type OrderItem struct {
	ID          uint    `gorm:"primaryKey" json:"id"`
	OrderID     uint    `gorm:"not null" json:"order_id"`                      // 订单ID
	ProductName string  `gorm:"size:200;not null" json:"product_name"`         // 商品名称
	ProductSpec string  `gorm:"size:100" json:"product_spec"`                  // 商品规格
	Quantity    int     `gorm:"not null" json:"quantity"`                      // 数量
	UnitPrice   float64 `gorm:"type:decimal(10,2);not null" json:"unit_price"` // 单价
	Amount      float64 `gorm:"type:decimal(10,2);not null" json:"amount"`     // 小计金额
	CreatedAt   time.Time
	UpdatedAt   time.Time
}

// OrderResponse 用于API响应的订单数据
type OrderResponse struct {
	ID          uint                `json:"id"`
	OrderNo     string              `json:"order_no"`
	Customer    UserResponse        `json:"customer"`
	Creator     UserResponse        `json:"creator"`
	Status      OrderStatus         `json:"status"`
	TotalAmount float64             `json:"total_amount"`
	Remark      string              `json:"remark"`
	OrderDate   time.Time           `json:"order_date"`
	CreatedAt   time.Time           `json:"created_at"`
	Items       []OrderItemResponse `json:"items"`
}

// OrderItemResponse 用于API响应的订单项数据
type OrderItemResponse struct {
	ID          uint    `json:"id"`
	ProductName string  `json:"product_name"`
	ProductSpec string  `json:"product_spec"`
	Quantity    int     `json:"quantity"`
	UnitPrice   float64 `json:"unit_price"`
	Amount      float64 `json:"amount"`
}

// ToResponse 将订单模型转换为响应格式
func (o *Order) ToResponse() OrderResponse {
	items := make([]OrderItemResponse, len(o.Items))
	for i, item := range o.Items {
		items[i] = OrderItemResponse{
			ID:          item.ID,
			ProductName: item.ProductName,
			ProductSpec: item.ProductSpec,
			Quantity:    item.Quantity,
			UnitPrice:   item.UnitPrice,
			Amount:      item.Amount,
		}
	}

	return OrderResponse{
		ID:          o.ID,
		OrderNo:     o.OrderNo,
		Customer:    o.Customer.ToResponse(),
		Creator:     o.Creator.ToResponse(),
		Status:      o.Status,
		TotalAmount: o.TotalAmount,
		Remark:      o.Remark,
		OrderDate:   o.OrderDate,
		CreatedAt:   o.CreatedAt,
		Items:       items,
	}
}

// CreateOrderRequest 创建订单请求
type CreateOrderRequest struct {
	CustomerID uint                     `json:"customer_id" binding:"required"`
	CreatorID  uint                     `json:"creator_id" binding:"required"`
	Remark     string                   `json:"remark"`
	OrderDate  time.Time                `json:"order_date" binding:"required"`
	Items      []CreateOrderItemRequest `json:"items" binding:"required,min=1"`
}

// CreateOrderItemRequest 创建订单项请求
type CreateOrderItemRequest struct {
	ProductName string  `json:"product_name" binding:"required"`
	ProductSpec string  `json:"product_spec"`
	Quantity    int     `json:"quantity" binding:"required,min=1"`
	UnitPrice   float64 `json:"unit_price" binding:"required,min=0"`
}

// CalculateAmount 计算订单项金额
func (item *OrderItem) CalculateAmount() {
	item.Amount = float64(item.Quantity) * item.UnitPrice
}

// CalculateTotalAmount 计算订单总金额
func (o *Order) CalculateTotalAmount() {
	total := 0.0
	for _, item := range o.Items {
		total += item.Amount
	}
	o.TotalAmount = total
}
